import { defineConfig, loadEnv } from 'vite';
import vue from '@vitejs/plugin-vue';
import VueJsx from '@vitejs/plugin-vue-jsx';
import AutoImport from 'unplugin-auto-import/vite';
import Components from 'unplugin-vue-components/vite';
import Unocss from 'unocss/vite';
import { AntDesignVueResolver } from 'unplugin-vue-components/resolvers';
import path from 'path';
// https://vitejs.dev/config/
export default ({ mode }) => {
  const env = loadEnv(mode, process.cwd());
  return defineConfig({
    plugins: [
      vue({
        script: {
          defineModel: true,
        },
      }),
      VueJsx(),
      AutoImport({
        imports: [
          'vue',
          'pinia',
          '@vueuse/core',
          'vue-router',
          {
            'ant-design-vue': ['message', 'modal', 'Form'],
            store2: [['default', 'st']],
            'lodash-es': ['cloneDeep', 'pick', 'omit'],
            classnames: [['default', 'cn']],
          },
        ],
        dts: './src/auto-imports.d.ts',
        dirs: ['./src/hooks', './src/utils', './src/types'],
        vueTemplate: true,
      }),
      Components({
        include: [
          /\.[tj]sx?$/, // .ts, .tsx, .js, .jsx
          /\.vue$/,
          /\.vue\?vue/, // .vue
        ],
        // extensions: ['vue', 'tsx'],
        resolvers: [
          AntDesignVueResolver({
            importStyle: false,
          }),
        ],
        dirs: [],
        dts: './src/auto-components.d.ts',
      }),
      Unocss(),
    ],
    resolve: {
      alias: {
        '@': path.resolve(__dirname, './src/'),
      },
    },
    server: {
      host: '0.0.0.0',
      proxy: {
        '/merger': {
          target: env.VITE_PROXY,
          changeOrigin: true,
          rewrite: path => path.replace(/^\/merger/, ''),
        },
        '/selection': {
          target: env.VITE_SELECTION_PROXY,
          changeOrigin: true,
          rewrite: path => path.replace(/^\/selection/, ''),
        },
      },
    },
    build: {
      outDir: 'dist',
      assetsDir: 'assets',
      terserOptions: {
        compress: {
          drop_console: true,
          drop_debugger: true,
        },
      },
      minify: 'terser',
    },
  });
};
